package plgbase

import (
	"dgo/goutils/utils"
	"gitee.com/ymofen/supervalue"
	"time"
)

/*
最简单的插件
1.插件创建释放过程，调试插件释放
*/
type HelloPlg struct {
	PlgBase
	//PlugId string
	msglst *utils.SyncCycleList
}

/*
	{
	  "id": "xxx",
	  "type": "demo.plg",
	  "conf": {

	  }
	}
*/
func (this *HelloPlg) ReloadConf(conf *wrapper.SuperValue) error {
	if conf == nil {
		return nil
	}

	if this.msglst == nil {
		this.msglst = utils.NewSyncCycleList()
	}

	utils.DefaultPatrolTask().AddTask(this, time.Second*30, func(id interface{}, args ...interface{}) {
		//utils.DefaultWorkers().PostTaskFunc(this, this.innerWork)
	})
	return nil
}

func (this *HelloPlg) Close() (err error) {
	this.msglst = nil
	this.PlgBase.Close()
	utils.DefaultPatrolTask().DelTask(this)
	return
}

func (this *HelloPlg) DoStatusRequestRequest(args ...interface{}) (resp interface{}) {
	return "hello world"
}

func (this *HelloPlg) LogInfoMessage(s string, args ...interface{}) {
	this.msglst.LogMsg(s, args...)
	utils.Log.Msgf(this.PlugId, utils.LogLV_INFO, 1, s, args...)
}

func (this *HelloPlg) LogWarnMessage(s string, args ...interface{}) {
	this.msglst.LogMsg(s, args...)
	utils.Log.Msgf(this.PlugId, utils.LogLV_WARN, 1, s, args...)
}

func CreateHelloPlgFunc(args ...interface{}) (rval interface{}, err error) {
	obj := &HelloPlg{}

	if len(args) > 0 {
		if str, ok := args[0].(string); ok {
			obj.PlugId = str
		}
	}
	rval = obj
	return
}
